package com.prp.gwt.client.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.IListener;

public class UserBaseListener implements IListener{
	
	private static UserBaseListener instance;
	
	public static UserBaseListener getInstance(){
		if(instance == null)
			instance = new UserBaseListener();
		return instance;
	}
	
	private UserBaseServiceAsync service = GWT.create(UserBaseService.class);

	public void signOut() {
		service.signOut(new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Void result) {
				Window.Location.replace(Host.getENTRY());
			}
		});
	}

	public void verify() {
		service.verify(new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result)
					UserEntryPoint.init();
				else
					Window.Location.replace(Host.getENTRY());
			}
		});
	}
	
}
